package org.ltsh.generate;

import com.alibaba.fastjson.JSONObject;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.ltsh.generate.config.GenerateConfig;
import org.ltsh.generate.config.GlobalConfig;
import org.ltsh.generate.config.ReadConfig;
import org.ltsh.generate.config.TempleteConfig;
import org.ltsh.generate.entity.GenerateEntity;
import org.ltsh.generate.entity.PackageGenerateEntity;

import org.ltsh.generate.templete.XmlTempleteUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by fengjianbo on 2017/12/26.
 */
@Mojo(name = "run")
public class GenerateMain extends AbstractMojo {

    @Parameter(defaultValue = "/templeteProject/autoConfig.xml")
    private String configPath;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("jdbc.properties"));
            GlobalConfig.jdbcConfig.setConnectionURL(properties.getProperty("connectionURL"));
            GlobalConfig.jdbcConfig.setDatabase(properties.getProperty("database"));
            GlobalConfig.jdbcConfig.setDriverClass(properties.getProperty("driverClass"));
            GlobalConfig.jdbcConfig.setJdbcJarPath(properties.getProperty("jdbcJarPath"));
            GlobalConfig.jdbcConfig.setPassword(properties.getProperty("password"));
            GlobalConfig.jdbcConfig.setUseInformationSchema(properties.getProperty("useInformationSchema"));
            GlobalConfig.jdbcConfig.setUseJdbc(properties.getProperty("useJdbc"));
            GlobalConfig.jdbcConfig.setUserName(properties.getProperty("userName"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("org/beetl/sample/s01/");
        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            e.printStackTrace();
        }
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("/hello.txt");
        String str = t.render();
        System.out.println(str);
        System.out.println("ConfigPath:" + configPath);
        try {


        } catch (Exception e) {
            System.out.println("error---------------------");
            e.printStackTrace();
        }
        System.out.println("执行完毕-------");
    }

}
